from django.shortcuts import render, redirect
# 导包
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
# 导入类视图
from django.views import View

from myapp.models import User
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
# 导入加密库
import hashlib
# 导入图片库
# 绘画库
from PIL import ImageDraw
# 字体库
from PIL import ImageFont
# 图片库
from PIL import Image
# 随机库
import random
# 文件流
import io

import requests

# 导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

# 导入原生sql模块
from django.db import connection

import jwt

# 导入redis数据库
import redis

# 导入时间模块
import time

# 导入公共目录变量
from mydjango.settings import BASE_DIR

# 导包
from django.db.models import Q, F

# 导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid


# 方法视图
def myindex(request):
    return HttpResponse('这里是首页')


# 类视图 可读性好，易于扩展
class MyView(View):
    greeting = "你好，这里是首页"

    def get(self, request):
        title = "hello django"
        num = 100
        li = [1, 2, 3, 6, 4]
        dic = {'name': '小红', 'age': 18}

        # return HttpResponse(self.greeting)
        # 渲染模板
        return render(request, 'index.html', locals())


# 用户添加接口
class AddUser(View):
    def get(self, request):
        users = User.objects.all()
        return render(request, 'adduser.html', locals())

    def post(self, request):
        username = request.POST.get('username')
        password = request.POST.get('password')
        img = request.POST.get('img')
        type = request.POST.get('type')
        user = User.objects.filter(username=username).first()
        if user:
            return HttpResponse("该用户已存在")
        User.objects.create(username=username, password=password, type=type, img=img)
        return redirect("/list")


# 用户展示接口
class List(View):
    def get(self, request):
        users = User.objects.all()
        return render(request, 'index.html', {'users': users})


# 删除用户接口
class Delete(View):
    def get(self, request):
        id = request.GET.get("id")
        try:
            User.objects.get(id=id).delete()
        except Exception as e:
            print(e)
            return HttpResponse("删除失败")
        return redirect("/list")


# 用户更新接口
class Update(View):
    def get(self, request):
        id = request.GET.get("id")
        user = User.objects.get(id=id)
        return render(request, 'update.html', {'user': user})

    def post(self, request):
        id = request.POST.get("id")
        username = request.POST.get("username")
        password = request.POST.get("password")
        img = request.POST.get("img")
        type = request.POST.get("type")
        try:
            user = User.objects.get(id=id)
            print(user)
            user.username = username
            user.password = password
            user.img = img
            user.type = type
            user.save()
        except Exception as e:
            print(e)
            return HttpResponse("更新失败")
        return redirect("/list")
